package com.bibrApp.difu.actvity.u_account_mvp;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.bibrApp.difu.R;
import com.bibrApp.difu.actvity.others.USettingActivity;
import com.bibrApp.difu.base.BaseActivity;
import com.bibrApp.difu.fragment.account_mvp.AccountFragment;
import com.bibrApp.difu.fragment.receiver_mvp.ReceiverFragment;
import com.bibrApp.difu.fragment.send_mvp.SendFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.OnClick;

public class UAccountActivity extends BaseActivity<UAccountPresenter> implements IUAccountView {
    public static final String[] tabs = {"账户", "发送", "接收"};

    List<Fragment> fragmentList = new ArrayList<>();
    @BindView(R.id.home_tab_layout)
    TabLayout tabLayout;

    @Override
    protected void initPresenter() {
        presenter = new UAccountPresenter(this, this);
        presenter.init();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_uaccount;
    }


    @Override
    public void error(String msg) {

    }

    @Override
    public void success(String msg) {

    }

    @Override
    public void initView() {
        fragmentList.add(new AccountFragment());
        fragmentList.add(new SendFragment());
        fragmentList.add(new ReceiverFragment());
        tabLayout.addTab(tabLayout.newTab().setCustomView(getTabView(0)),false);
        tabLayout.addTab(tabLayout.newTab().setCustomView(getTabView(1)),false);
        tabLayout.addTab(tabLayout.newTab().setCustomView(getTabView(2)),false);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                updateTabTextView(tab, true);
                Fragment fragment = fragmentList.get(tab.getPosition());
                smartReplaceFragment(R.id.fl_container, fragment, String.format("%s%s", fragment.getClass().getSimpleName(), tab.getPosition()));
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                updateTabTextView(tab, false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        Objects.requireNonNull(tabLayout.getTabAt(0)).select();

    }


    private View getTabView(int currentPosition) {
        View view = LayoutInflater.from(this).inflate(R.layout.tab_account, null);
        TextView textView = view.findViewById(R.id.tab_item_textview);
        textView.setText(tabs[currentPosition]);
        return view;
    }

    private void updateTabTextView(TabLayout.Tab tab, boolean isSelect) {
        if (isSelect) {
            TextView tabSelect = tab.getCustomView().findViewById(R.id.tab_item_textview);
            tabSelect.setTextAppearance(this, R.style.TabLayoutTextStyle);
            tabSelect.setTextColor(getResources().getColor(R.color.blue_e4));
        } else {
            TextView tabUnSelect = tab.getCustomView().findViewById(R.id.tab_item_textview);
            tabUnSelect.setTextAppearance(this, R.style.TabLayoutUnTextStyle);
            tabUnSelect.setTextColor(getResources().getColor(R.color.black_a7));
        }
    }

    @OnClick(R.id.bt_back)
    public void backClick() {
        finish();
    }

    @OnClick(R.id.btn_setting)
    public void settingClick() {
        startActivity(USettingActivity.class);
    }
}
